#include "RouteTable.h"

int getDestinationAddressFromRouteTable(unsigned char* nodeID, unsigned char destHigh[4], unsigned char destLow[4])
{
    int iCpt = 0;
    int iIndex = tryGetEntryIndex(nodeID);
    if(iIndex < 0)
    {
        return 0;
    }
    else
    {
        for(iCpt = 0 ; iCpt < 4 ; iCpt ++)
        {
            destHigh[iCpt] = gl_RouteTable.Entries[iIndex].entry_64BitsAddress_HIGH[iCpt];
            destLow[iCpt] = gl_RouteTable.Entries[iIndex].entry_64BitsAddress_LOW[iCpt];
        }
        return 1;
    }
}

int tryGetEntryIndex(unsigned char* nodeID)
{
    char iCpt = 0;
    for(iCpt = 0 ; iCpt < gl_RouteTable.curTableIndex ; iCpt ++)
    {
        if (stringCompare(gl_RouteTable.Entries[iCpt].entry_NI, nodeID))
        {
            return iCpt;
        }
    }
    return -1;
}

int tryGetEntryIndexFromAddress(unsigned char destH[4], unsigned char destL[4])
{
    char iCpt = 0;
    char iCpt2 = 0;
    for(iCpt = 0 ; iCpt < gl_RouteTable.curTableIndex ; iCpt ++)
    {
        for(iCpt2 = 0 ; iCpt2 < 4 ; iCpt2 ++)
        {
            if(((unsigned char)gl_RouteTable.Entries[iCpt].entry_64BitsAddress_HIGH[iCpt2] != (unsigned char)destH[iCpt2])||((unsigned char)gl_RouteTable.Entries[iCpt].entry_64BitsAddress_LOW[iCpt2] != (unsigned char)destL[iCpt2]))
            {
                break;
            }
            if(iCpt == 3)
            {
                return iCpt;
            }
        }
    }
    return -1;
}

int addRouteTableEntry(unsigned char* nodeID, unsigned char destHigh[4], unsigned char destLow[4])
{
    int iIndex;
    if(gl_RouteTable.curTableIndex == MAX_SENSOR)
    {
        return 0;
    }
    iIndex = tryGetEntryIndex(nodeID);
    if(iIndex < 0)
    {
        stringCopy(nodeID,gl_RouteTable.Entries[gl_RouteTable.curTableIndex].entry_NI);
        for(iIndex = 0 ; iIndex < 4 ; iIndex ++)
        {
            gl_RouteTable.Entries[gl_RouteTable.curTableIndex].entry_64BitsAddress_HIGH[iIndex] = destHigh[iIndex];
            gl_RouteTable.Entries[gl_RouteTable.curTableIndex].entry_64BitsAddress_LOW[iIndex] = destLow[iIndex];
        }
        gl_RouteTable.curTableIndex ++;
    }

    return 1;
}

int removeRouteTableEntry(unsigned char* nodeID)
{
    int iCpt;
    int iIndex;
    iIndex = tryGetEntryIndex(nodeID);
    if(iIndex < 0)
    {
        return 0;
    }
    else
    {
        gl_RouteTable.curTableIndex --;
        while(iIndex < gl_RouteTable.curTableIndex)
        {
            stringCopy(gl_RouteTable.Entries[iIndex+1].entry_NI,gl_RouteTable.Entries[iIndex].entry_NI);
            for(iCpt = 0 ; iCpt < 4 ; iCpt++)
            {
                gl_RouteTable.Entries[iIndex].entry_64BitsAddress_HIGH[iCpt] = gl_RouteTable.Entries[iIndex + 1].entry_64BitsAddress_HIGH[iCpt];
                gl_RouteTable.Entries[iIndex].entry_64BitsAddress_LOW[iCpt] = gl_RouteTable.Entries[iIndex + 1].entry_64BitsAddress_LOW[iCpt];
            }
            iIndex++;
        }
        return 1;
    }
}

void displayRouteTable()
{

    int iCpt = 0;
    int iCpt2 = 0;

    printf("\n%d entrees dans la table : \n", gl_RouteTable.curTableIndex);
    printf("\tNodeID\tDestHIGH\tDestLOW\t\tErrCount\n");

    for(iCpt = 0 ; iCpt < gl_RouteTable.curTableIndex ; iCpt ++)
    {
        printf("\t%.3s\t", gl_RouteTable.Entries[iCpt].entry_NI);
        for(iCpt2 = 0 ; iCpt2 < 4 ; iCpt2 ++)
        {
             printf("%x ",gl_RouteTable.Entries[iCpt].entry_64BitsAddress_HIGH[iCpt2]);
        }
        putchar('\t');
        for(iCpt2 = 0 ; iCpt2 < 4 ; iCpt2 ++)
        {
             printf("%x ",gl_RouteTable.Entries[iCpt].entry_64BitsAddress_LOW[iCpt2]);
        }
        putchar('\t');
        printf("%d ",(unsigned char)gl_RouteTable.Entries[iCpt].entry_ErrorCount);
        putchar('\n');
    }
}

void incrementAllErrorCount()
{
    int iCpt;
    for(iCpt = 0 ; iCpt < gl_RouteTable.curTableIndex ; iCpt ++)
    {
        incrementErrorCount(iCpt);
    }
}

void incrementErrorCount(int iIndex)
{
    gl_RouteTable.Entries[iIndex].entry_ErrorCount ++;
}
